﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using Htss.Platform.WebApi;
using Htss.Platform.Core;

namespace DreamBox.Api
{
    public class loginBox:ApiBox
    {
        public ActionResult post()
        {
            Verify vf=Verification();
            if (vf.IsVerify)
            {
                string UserName = Request.Form["UserName"];
                string Password = Request.Form["Password"];
                string IsCookie = Request.Form["IsCookie"];

                DAL.User.Login login = new DAL.User.Login();

                DAL.PSelect ps = new DAL.PSelect();
                DataTable dt = login.UserLogin(UserName, FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5"));
                if (dt.Rows.Count > 0)
                {
                    if (IsCookie != "" && IsCookie == "true")
                    {
                        BLL.User.Cookies.Add(this.Response, UserName, FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5"));
                    }
                    //存储用户基本信息
                    this.Session["UserID"] = dt.Rows[0]["UserID"];
                    this.Session["UserName"] = dt.Rows[0]["UserName"];
                    this.Session["Avatar"] = dt.Rows[0]["Avatar"];
                    this.Session["Location"] = dt.Rows[0]["Location"];
                    //清空不登录时的权限列表信息
                    if (this.Session["GroupCompetenceData"] != null)
                    {
                        this.Session.Remove("GroupCompetenceData");
                    }
                    //更新登录后的用户组信息
                    BLL.User.Competence.ComGroupRight group = new BLL.User.Competence.ComGroupRight();
                    group.SetCompetence(System.Web.HttpContext.Current, dt);

                    return Json(true, "登陆成功");
                }
                else
                {
                    return Json(false, "用户名或密码错误");
                }
            }
            else
            {
                return Json(false, vf.VerifyDt);
            }
        }
        Verify Verification()
        {
            Verify vf = new Verify();
            VerifyField vUserName = vf.NewVerifyField(Request.Form["UserName"], "UserName");
            vUserName.AddNull("请输入用户名");
            vUserName.AddLengthRange("长度范围在1-20之间", 1, 20);

            VerifyField vPassword = vf.NewVerifyField(Request.Form["Password"], "Password");
            vPassword.AddNull("此项不能为空");
            vPassword.AddLengthRange("请输入(6-20)之间的密码长度", 6, 20);



            vf.Begin();
            return vf;
        }
    }
}